package com.jimo.transadb.utils;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;

import androidx.core.app.NotificationCompat;
import androidx.core.app.RemoteInput;

import com.jimo.transadb.App;
import com.jimo.transadb.Constant;
import com.jimo.transadb.InputPairReceiver;

public class NotificationUtils {

    public static void showNotifyIpaddress(Context context, boolean isIpAddress) {
        createNotificationChannel(context);
        Intent intent = new Intent(context, InputPairReceiver.class);
        if (isIpAddress) {
            intent.putExtra("isIpAddress", true);
        }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, Constant.notifyIdName)
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setContentTitle("请到无线调试中打开配对码弹窗查看")
                .setContentText("请输入IP地址和端口")
                .addAction(new NotificationCompat.Action.Builder(android.R.drawable.ic_menu_send, "回复",
                        PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_MUTABLE))
                        .addRemoteInput(new RemoteInput.Builder(Constant.notifyIdName)
                                .setLabel("请输入配对码")
                                .build())
                        .build());
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(Constant.channelId, builder.build());
        startDevelopmentActivity(context);
    }

    public static void dismissNotifyByChannelId(int channelId) {
        NotificationManager notificationManager = (NotificationManager) App.instance.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(channelId);
    }

    static void createNotificationChannel(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(Constant.notifyIdName, Constant.notifyIdName, importance);
            channel.setDescription(Constant.notifyIdName);
            NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

    public static void startDevelopmentActivity(Context context) {
        try {
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
            context.startActivity(intent);
        } catch (Exception e) {
            try {
                ComponentName componentName = new ComponentName("com.android.settings", "com.android.settings.DevelopmentSettings");
                Intent intent = new Intent();
                intent.setComponent(componentName);
                intent.setAction("android.intent.action.View");
                context.startActivity(intent);
            } catch (Exception e1) {
                try {
                    Intent intent = new Intent("com.android.settings.APPLICATION_DEVELOPMENT_SETTINGS");//部分小米手机采用这种方式跳转
                    context.startActivity(intent);
                } catch (Exception ignored) {

                }

            }
        }
    }
}